package com.dkd.manage.service.impl;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理Service业务层处理
 * 
 * @author itheima
 * @date 2024-10-13
 */
@Service
@Transactional
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private NodeMapper nodeMapper;   //点位表

    @Autowired
    private ChannelMapper channelMapper;  //货道表

    @Autowired
    private ChannelServiceImpl channelService;
    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        // 1. 新增设备
        // 生成8位设备唯一标识，补充货道编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);

        //设备容量--通过设备类型表获取
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());

        //查询点位表，补充冗余字段
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setRegionId(node.getRegionId());

        //设备状态，默认未投入
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY); //未投放
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());

        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //2.新增货道：行*列 个货道
        //货道集合：需要创建多个货道
        List<Channel> channelList = new ArrayList<>();
        for(int i=1;i<=vmType.getVmRow();i++){
            for(int j=1;j<=vmType.getVmCol();j++){
                //创建货道
                Channel channel = new Channel();
                //封装货道信息
                channel.setChannelCode(i+"-"+j);   //1-1  1-2
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vendingMachine.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }

        }

        //批量添加货道
        channelService.batchInsertChannel(channelList);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if (vendingMachine.getNodeId()!=null) {
            //获取修改后的点位：
            Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
            //更新冗余字段
            BeanUtils.copyProperties(node,vendingMachine,"id");
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据设备编号查找设备
     * @param innerCode
     * @return 设备信息
     */
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
